#include "pch.h"
#include "Reflection.h"
#include "Actor.h"
#include "Scene.h"
#include "Callback.h"
#include "ReflectionEffect.h"

/////////////////////////////////////////////////
class RefleCallBack : public mini::CCallBack
{
public:
	RefleCallBack()
	{

	}
	RefleCallBack(void* pParameters)
		:mini::CCallBack(pParameters)
	{

	}

	virtual void onRun(void* pParameters)
	{
		static CVector r;
		r.x = 0;
		r.y += 0.0001f;
		r.z = 0;
	}
	mini::CReflectionEffect* mpActor = nullptr;
};



////////////////////////////////////////////////////////////
CRefActor::CRefActor()
{
}

CRefActor::~CRefActor()
{
}

void CRefActor::init()
{
	auto pScene = mini::GEngine->getCurrentScene();
	mini::CReflectionEffect* pRefActor = new mini::CReflectionEffect();

	pRefActor->setName("modelPreviewViewport");
	pRefActor->createBuildCamera("previewCamera", mini::CPoint3F(0, -3, -10), mini::CPoint3F(0, -3, -9));
	pRefActor->initViewPortSize(mini::GEngine->getDeviceWidth(), mini::GEngine->getDeviceHeight());
	pRefActor->setRendererFlag(mini::RendererType::FrontRenderer);

	for (auto item : pScene->getRenderNodes())
	{
		auto renderNode = item->Clone();
		pRefActor->addRenderNode(renderNode);
	}
	pScene->addActor(pRefActor);

	pRefActor->initialise("resource/Mesh/floor.txt", "resource/Material/Reflec.ma");
	mini::CRenderNode* pRenderNode = mini::GEngine->createRenderNode();
	pRenderNode->bindActor(pRefActor, mini::RendererType::FrontRenderer);
	mini::GEngine->getCurrentScene()->addRenderNode(pRenderNode);
	pRefActor->setPosition(CVector(0, -1, 0));
	auto& pMeshUnits = pRefActor->getMeshUnits();
	for (auto item : pMeshUnits)
	{
		auto pUnit = item->getMaterialUnit();
		pUnit->setTexture(pRefActor->getRenderTexture(), 1);
	}
//	SafeRelease(pRefActor);
}


void CRefActor::updateFrame(float fDeltaTime)
{
//	auto pMatrxi = mpRenderViewport->getCamera()->getViewMatrix();
//	mpMeshUnits.front()->getMaterialUnit()->updateVSConstBuffer(5, pMatrxi);
}

///////////////////////////////////////////////////
CReflection::CReflection(mini::CEngine* pEngine)
{
	CRefActor* pActor = new CRefActor;
	pActor->initialise("resource/Mesh/Default/cube.m", "resource/Material/clipPlaneMaterial.ma");
	mini::CRenderNode* pRenderNode = pEngine->createRenderNode();
	pRenderNode->bindActor(pActor, mini::RendererType::FrontRenderer);
	pEngine->getCurrentScene()->addRenderNode(pRenderNode);
	pActor->setPosition(CVector(0, 0, 0));
	mpActor = pActor;


	mRefActor.init();

	RefleCallBack* pRefCallBack = new RefleCallBack();
	pRefCallBack->mAlternationTime = 1000;
//	pRefCallBack->mpActor = &mRefActor;
	mini::CTimerEngine::getSinglePtr()->registerCall(pRefCallBack);
	SafeRelease(pActor);
}

CReflection::~CReflection()
{
	mpActor = nullptr;
}




